<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/12
 * Time: 14:21
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\common\model\Messagem;
use app\common\model\User;
use think\Cache;
use think\captcha\Captcha;

class Login extends HomeBase
{
    /**
     * 注册
     */
    public function register()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wregister');
            if ($validate_result !== true) {
                return json(['code' => 300, 'msg' => $validate_result]);
            }
//            if (Cache::get($params['u_email']) != $params['u_email_code']) {
//                return json(['code' => 302, 'msg' => '邮箱验证码错误']);
//            }
            if (User::emailRepace($params['u_email'])) {
                return  json(['code' => 303, 'msg' => '该账号已注册！']);
            }
            $params['u_superior'] = User::getCode($params['u_code']);
            if (!$params['u_superior']) {
                return  json(['code' => 303, 'msg' => '邀请码错误']);
            }
            $id = User::registerUser($params);
            if ($id) {
                return json(['code' => 200, 'msg' => '注册成功']);
            } else {
                return  json(['code' => 303, 'msg' => '注册失败，请稍后重试！']);
            }
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 忘记密码
     */
    public function  retrieve()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Rretrieve');
            if ($validate_result !== true) {
                return json(['code' => 300, 'msg' => $validate_result]);
            }
            if (Cache::get($params['u_email']) != $params['u_email_code']) {
                return json(['code' => 302, 'msg' => '邮箱验证码错误']);
            }
            $id = User::forgetPassword($params);
            if ($id) {
                return json(['code' => 200, 'msg' => '重置密码成功']);
            } else {
                return  json(['code' => 303, 'msg' => '操作失败，请稍后重试！']);
            }
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 登录
     */
    public function login()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wlogin');
            if ($validate_result !== true) {
               return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $data = User::loginUser($params);
            if ($data === -1) {
                return json(['code' => 301, 'msg' => '用户名不存在，请先注册账号']);
            }
            if ($data === -2) {
                return json(['code' => 301, 'msg' => '密码错误']);
            }
            if ($data === -3) {
                return json(['code' => 301, 'msg' => '该账号已被禁止登陆']);
            }
            if ($data === -4) {
                return json(['code' => 301, 'msg' => '平台用户禁止移动端登陆']);
            }
            return json(['code' => 200, 'msg' => $data]);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }


    /**
     * 通过邮箱找回密码
     */
    public function findPassWordForEmail()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wfindpassword');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            if (Cache::get($params['u_email']) != $params['u_email_code']) {
                return json(['code' => 302, 'msg' => '邮箱验证码错误']);
            } else {
                return json(['code' => 200, 'msg' => $params['u_email']]);
            }
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }
    /**
     * 重置密码
     */
    public function resetPassWord()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wresetpassword');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            if (Cache::get($params['u_email']) != $params['u_email_code']) {
                return json(['code' => 302, 'msg' => '邮箱验证码错误']);
            }
            //通过邮箱修改密码 $account, $u_password, $type
            if (User::resetPasswordForAccount($params['u_email'], $params['u_password'], 1)){
                return json(['code' => 200, 'msg' => '重置密码成功']);
            } else {
                return json(['code' => 302, 'msg' => '重置密码失败']);
            }
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 生成验证码
     */
    public function verify()
    {
        $captcha = new Captcha();
        return $captcha->entry();
    }

    /**
     * 修改密码
     */
    public function updatePassword()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wupdatepassword');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            if ($params['oldpassword'] == $params['u_password']) {
                return  json(['code' => 302, 'msg' => '修改密码不能和原密码一致']);
            }
            $id = User::savePassword($params['u_id'], $params['oldpassword'], $params['u_password']);
            if ($id === -1) {
                return  json(['code' => 302, 'msg' => '旧密码输入错误']);
            }
            if ($id) {
                return  json(['code' => 200, 'msg' => '密码修改成功']);
            }
            return  json(['code' => 303, 'msg' => '密码修改失败']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 查看用户当前有没有领取签到的积分
     */
    public function getUserReceivePoints()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $data = Messagem::getUserDayPoints($params['u_id']);
            return json(['code' => 200, 'msg' => $data]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }
    /**
     * 用户领取签到积分
     */
    public function receivePoints()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $status = Messagem::userReceive($params['u_id']);
            if (!$status) {
                return json(['code' => 301, 'msg' => '签到失败']);
            }
            return json(['code' => 200, 'msg' => '签到成功']);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }
    /**
     * 积分变更列表
     */
    public function getUserPointsList($page = 1)
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $data = Messagem::PointsList($params['u_id'], $page);
            if (!$data) {
                return json(['code' => 301, 'msg' => '暂无数据']);
            }
            return json(['code' => 200, 'msg' => $data]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }

}